﻿using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command.PermissionCommand;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
    /// <summary>
    /// 权限接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class PermissionController : ControllerBase
    {
        private readonly IMediator _mediatR;
        private readonly IPermissionService _permissionService;

        public PermissionController(IPermissionService permissionService, IMediator mediatR)
        {
            _permissionService = permissionService;
            _mediatR = mediatR;
        }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public Results<List<MenuOutput>> GetMenuList(int pid)
        {
            var list = _permissionService.GetMenuList(pid);
            return new Results<List<MenuOutput>>()
            {
                Code = 1,
                Data = list,
                Msg = "获取菜单列表成功"
            };
        }
        /// <summary>
        /// 获取角色权限列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<List<Permission>>> GetRolesPermission(int RoleId)
        {
            var command = new RolePermissionCommand(RoleId);
            return await _mediatR.Send(command);
        }
        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public Results<object> SavePermission(UpdRolePermissionInput input)
        {
            int n = _permissionService.SavePermission(input);
            return Results<object>.DataResult(n);
        }
    }
}
